package com.javid.study;

import org.junit.platform.commons.util.StringUtils;

import java.util.*;
import java.util.regex.Pattern;

public class NYN_Demo {
    private static List<String> poker;
    private static String[] savePoker = new String[]{"A", "5", "8", "10"};
    private static String[] savePokerNeedDelete = new String[]{"2", "K"};
    private static String[] comeon = new String[]{"9", "J", "Q"};
    private static String[] nameList;
    private static String whohas2;
    private static String whohasK;
    private static LinkedHashMap<String, List<String>> namePoker = new LinkedHashMap<>();
    private static LinkedHashMap<String, Integer> scoreList = new LinkedHashMap<>();


    public static void puke() {
        List<String> puke = new ArrayList<>();
        //定义数字
        String[] nums = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"};
        //定义花色
        String[] color = {"♠", "♥", "♣", "♦",};
        for (String num : nums) {
            for (String s : color) {
                String poker = s + num;
                puke.add(poker);
            }
        }
        poker = puke;
    }

    public static String getPoker(List<String> poker) {
        Random random = new Random();
        int x = random.nextInt(poker.size());
        String get = poker.get(x);
        poker.remove(get);
        return get;
    }

    public static void func(String[] nameList, LinkedHashMap<String, List<String>> namePoker, String number){
        System.out.print("输入玩家编号(0为功能项)：");
        String n = new Scanner(System.in).next();
        if (Pattern.matches("[0-9]", n) && Integer.parseInt(n) <= Integer.parseInt(number)) {
            if (n.equals("0")) {
                System.out.print("删牌人员编号，牌序号。加杯人员编号：");
                String s = new Scanner(System.in).next();
                // 删牌
                String who1 = nameList[Integer.parseInt(s.split("。")[0].split("，")[0]) - 1];
                String poke = s.split("。")[0].split("，")[1];
                namePoker.get(who1).removeIf(k -> k.split("")[1].equals(poke));
                // 加杯
                String who2 = nameList[Integer.parseInt(s.split("。")[1]) - 1];
                scoreList.put(who2, scoreList.get(who2) + 1);
                if (StringUtils.isNotBlank(whohas2) && !whohas2.equals(who2)) {
                    scoreList.put(whohas2, scoreList.get(whohas2) + 1);
                }
            } else {
                String poke = getPoker(poker);
                System.out.println(poke);
                String key = nameList[Integer.parseInt(n) - 1];
                if (Arrays.asList(savePoker).contains(poke.split("")[1])) {
                    if (poke.split("")[1].equals("A")) {
                        scoreList.put(key, scoreList.get(key) - 1);
                    }
                    namePoker.get(key).add(poke);
                } else if (Arrays.asList(savePokerNeedDelete).contains(poke.split("")[1])) {
                    if (poke.split("")[1].equals("2")) {
                        if (StringUtils.isNotBlank(whohas2)) {
                            namePoker.get(whohas2).removeIf(k -> k.split("")[1].equals("2"));
                        }
                        whohas2 = key;
                    }
                    namePoker.get(key).add(poke);
                } else if (Arrays.asList(comeon).contains(poke.split("")[1])) {
                    scoreList.put(key, scoreList.get(key) + 1);
                    if (StringUtils.isNotBlank(whohas2) && !whohas2.equals(key)) {
                        scoreList.put(whohas2, scoreList.get(whohas2) + 1);
                    }
                }
            }
        } else {
            System.out.println("输入有误！");
        }
    }
    public static void main(String[] args) {
        System.out.print("输入参与人数：");
        Scanner input = new Scanner(System.in);
        String number = input.next();
        if (Pattern.matches("[1-9]", number)) {
            System.out.print("依次录入名字，逗号分割：");
            String inputName = new Scanner(System.in).next();
            nameList = inputName.split("，");
            for (int i = 0; i < nameList.length; i++) {
                nameList[i] = i + 1 + nameList[i];
            }
            //LinkedHashMap有序

            for (int i = 0; i < Integer.parseInt(number); i++) {
                namePoker.put(nameList[i], new ArrayList<>());
                scoreList.put(nameList[i], 0);
            }
            puke();
            while (poker.size() > 0) {
                for (String j : namePoker.keySet()) {
                    System.out.print(j + namePoker.get(j) + ";");
                }
                System.out.println("本轮小姐" + (StringUtils.isNotBlank(whohas2) ? ":" + whohas2 : "暂无"));
                System.out.println("战绩展示：");
                for (String name : scoreList.keySet()) {
                    System.out.println(name + ":" + scoreList.get(name));
                }
                func(nameList, namePoker, number);
            }
            System.out.print("进入结算前，请确认是否需要操作？1是0否  ");
            String isorno = new Scanner(System.in).next();
            if (isorno.equals("1")){
                System.out.print("删牌人员编号，牌序号。加杯人员编号：");
                String s = new Scanner(System.in).next();
                // 删牌
                String who1 = nameList[Integer.parseInt(s.split("。")[0].split("，")[0]) - 1];
                String poke = s.split("。")[0].split("，")[1];
                namePoker.get(who1).removeIf(k -> k.split("")[1].equals(poke));
                // 加杯
                String who2 = nameList[Integer.parseInt(s.split("。")[1]) - 1];
                scoreList.put(who2, scoreList.get(who2) + 1);
                if (StringUtils.isNotBlank(whohas2) && !whohas2.equals(who2)) {
                    scoreList.put(whohas2, scoreList.get(whohas2) + 1);
                }
            }
            System.out.println("游戏结束。。。");
            System.out.println("战绩展示：");
            for (String name : scoreList.keySet()) {
                System.out.println(name + ":" + scoreList.get(name));
            }

        } else {
            System.out.println("输入非数字！");
        }
    }
}
